//
//  SWDataPkgViewController.m
//  DataTransfer
//
//  Created by 宋润理 on 2023/12/16.
//  Copyright © 2023 iCocos. All rights reserved.
/*
 1. 准备数据类型。图片，视频，联系人。
 2. 获取权限。
 3. 计算要发送的数据量。
 4. 可选择发送的数据。
 5. 默认全部发送。
 */

#import "SWDataPkgViewController.h"
#import "SWSendModel.h"
#import "AssetHelper.h"
#import "SendFileViewController.h"

@interface SWDataPkgViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *subTitle;
@property (nonatomic, strong) UITableView *dataList;
@property (nonatomic, strong) NSMutableArray<SWSendModel *> *dataArray;
@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation SWDataPkgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self displayUI];
    [self getData];
    [self.dataList reloadData];
}

#pragma mark - private

- (void)getData {
    SWSendModel *all = [[SWSendModel alloc] init];
    all.dataType = SendDataTypeAll;
    all.dataDesc = [NSString stringWithFormat:@"全选"];
    all.isSelected = YES;
    [self.dataArray addObject:all];
    
    SWSendModel *image = [[SWSendModel alloc] init];
    image.dataType = SendDataTypeImage;
    image.dataDesc = [NSString stringWithFormat:@"图片共：%@张",@(ASSETHELPER.allPhotos.count)];
    image.isSelected = YES;
    [self.dataArray addObject:image];
    
    SWSendModel *video = [[SWSendModel alloc] init];
    video.dataType = SendDataTypeVideo;
    video.dataDesc = [NSString stringWithFormat:@"视频共：%@条",@(ASSETHELPER.allVideos.count)];
    video.isSelected = YES;
    [self.dataArray addObject:video];
    
    SWSendModel *contact = [[SWSendModel alloc] init];
    contact.dataType = SendDataTypeContact;
    contact.dataDesc = [NSString stringWithFormat:@"联系人共：%@个",@(ASSETHELPER.allContacts.count)];
    contact.isSelected = YES;
    [self.dataArray addObject:contact];
}

- (void)displayUI {
    self.title = @"准备传输";
    [self setLeftItemWithIcon:[UIImage imageNamed:@"back"] title:nil selector:@selector(onClickBack)];
    
    UIImageView *mainImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Apple1024"]];
    mainImage.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:mainImage];
    [mainImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(25));
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.height.equalTo(@(100));
    }];
    
    [self.view addSubview:self.subTitle];
    [self.subTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(mainImage.mas_bottom).offset(25);
        make.left.equalTo(@(25));
    }];
    
    [self.view addSubview:self.sendBtn];
    [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(25));
        make.right.bottom.equalTo(@(-25));
        make.height.equalTo(@(50));
    }];
    
    [self.view addSubview:self.dataList];
    [self.dataList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.sendBtn);
        make.top.equalTo(self.subTitle.mas_bottom).offset(20);
        make.bottom.equalTo(self.sendBtn.mas_top).offset(-20);
    }];
}

#pragma mark - actions
- (void)onClickBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onClickSend {
    SendFileViewController *sendFileVC = [[SendFileViewController alloc] init];
    if (self.dataArray[1].isSelected) {
        sendFileVC.sendPhoto = YES;
    }
    if (self.dataArray[2].isSelected) {
        sendFileVC.sendVideo = YES;
    }
    if (self.dataArray[3].isSelected) {
        sendFileVC.sendContact = YES;
    }
    
    [self.navigationController pushViewController:sendFileVC animated:YES];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SWSendModel *cellData = self.dataArray[indexPath.row];
    cell.textLabel.text = cellData.dataDesc;
    cell.imageView.image = cellData.isSelected?[UIImage imageNamed:@"check_selected"]:[UIImage imageNamed:@"check_normal"];
    cell.contentView.backgroundColor = UIColorFromHex(0xf6f6f6);

    [cell.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.centerY.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.imageView.mas_right).offset(15);
        make.centerY.equalTo(@(0));
    }];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SWSendModel *cellData = self.dataArray[indexPath.row];
    if (indexPath.row == 0) {
        BOOL select = !cellData.isSelected;
        for (SWSendModel *cellData in self.dataArray) {
            cellData.isSelected = select;
        }
    }else{
        cellData.isSelected ^= 1;
    }
    [tableView reloadData];
}

#pragma mark - getter

- (UILabel *)subTitle{
    if(!_subTitle){
        _subTitle = [[UILabel alloc] init];
        _subTitle.font = [UIFont systemFontOfSize:18];
        _subTitle.text = @"请选择要传输的数据";
        
    }
    return _subTitle;
}

- (UITableView *)dataList{
    if(!_dataList){
        _dataList = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _dataList.delegate = self;
        _dataList.dataSource = self;
        [_dataList registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
        _dataList.separatorStyle = UITableViewCellSeparatorStyleNone;
        _dataList.backgroundColor = UIColorFromHex(0xf6f6f6);
        _dataList.bounces = NO;
    }
    return _dataList;
}

- (NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (UIButton *)sendBtn{
    if(!_sendBtn){
        _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sendBtn setTitle:@"开始发送" forState:UIControlStateNormal];
        [_sendBtn setBackgroundColor:MainThemeColor];
        _sendBtn.layer.cornerRadius = 25;
        _sendBtn.layer.masksToBounds = YES;
        [_sendBtn addTarget:self action:@selector(onClickSend) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendBtn;
}


@end
